﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Ki.GoogleEarth;

namespace Ki.Odin
{
    /// <summary>
    /// Main class to handle The application
    /// </summary>
    public sealed class FT2GEHandler : INotifyPropertyChanged
    {
        private ObservableCollection<Wrapper<string>> FractalLocations { get; set; }
        private string FinalLocation { get; set; }

        public FT2GEHandler()
        {
            int vCounter;
            FractalLocations = new ObservableCollection<Wrapper<string>>();

            for (vCounter = 0; vCounter < 6; vCounter++)
            {
                FractalLocations.Add(new Wrapper<string> { Value = "" });
            }

            FinalLocation = "";
        }

        public void Save_WhenClick(object tSender, RoutedEventArgs tArgs)
        {
            var vDialog = new FolderBrowserDialog { Description = "Where do you want this mess to go?" };
            if (vDialog.ShowDialog() != DialogResult.OK)
                return;

            FinalLocation = vDialog.SelectedPath;
            for (var vCounter = 0; vCounter < 6; vCounter++)
            {
                SaveLayer(FractalLocations[vCounter].Value, vCounter);
            }

            var vFile = KMLParts.SuperOverlay;
            vFile.Save(FinalLocation + "\\Base.kml");
        }

        public void SelectFractalLocation(object tSender, RoutedEventArgs tArgs)
        {
            var vDialog = new FolderBrowserDialog
                              {
                                  Description = "Select where the Fractal Terrain files are."
                              };
            if (vDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (var vLocation in FractalLocations)
                {
                    vLocation.Value = vDialog.SelectedPath;
                }
            }
        }

        private void SaveLayer(string tName, int tLayer)
        {
            var vSections = 1;
            for (var vCount = 0; vCount < tLayer; vCount++)
            {
                vSections *= 2;
            }
            var vLayerLocation = FinalLocation + "\\Layer" + tLayer;

            if (!Directory.Exists(vLayerLocation))
            {
                Directory.CreateDirectory(vLayerLocation);
            }

            for (var vX = 0; vX < vSections; vX++)
            {
                Parallel.For(0, vSections, tY =>
                                               {
                                                   if (
                                                       File.Exists(FractalLocations[tLayer].Value + "\\" +
                                                                   FractalTerrains.Utilities.FileName(tLayer, vX, tY) +
                                                                   ".jpg"))
                                                   {
                                                       var vFileLocation = "\\Layer" + tLayer + "\\" +
                                                                           FractalTerrains.Utilities.FileName(tLayer, vX,
                                                                                                              tY);
                                                       //Copy the Fractal Output.
                                                       File.Copy(
                                                           FractalLocations[tLayer].Value + "\\" +
                                                           FractalTerrains.Utilities.FileName(tLayer, vX, tY) +
                                                           ".jpg",
                                                           FinalLocation + "\\" + vFileLocation + ".jpg",
                                                           true);

                                                       //Create the KML File for the Image.
                                                       var vFile = KMLParts.LayerFile(tName, tLayer, vX, tY, vSections,
                                                                                      -1, 256);
                                                       vFile.Save(FinalLocation + "\\" + vFileLocation + ".kml");
                                                   }
                                               });
            }
        }
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void Changed(string tProperty)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(tProperty));
        }
        #endregion
    }
}
